#include "elec_show_outelec.h"

elec_Show_Outelec::elec_Show_Outelec(QWidget *parent) : QMainWindow(parent)
{
    Sysctrl->SYS_CTL_MODE =0;
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
    this->setFixedSize(640,430);
    timer = new QTimer(this);
    timer->start(1000);

    UI_Init();
    connect_Init();
    update_data();

}

void elec_Show_Outelec::UI_Init()
{

    the_btn_style[0]=("\
                      outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                      background-color: rgb(0, 170, 255);	    /* 背景颜色 */\
                      border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                      border-style: outset;\
                      /* 按钮背景图标设置 */\
                      background-image: url(:/pic/access/气象站.png); /* 背景图片 */\
                      background-origin: content;\
                      background-position: left;	         /* 背景图片的位置 */\
                      padding-right: 0px;	    /*内边距设置*/	    \
                      padding-left: 10px;		/*  */	\
                      padding-bottom: 10px;		/*  */    \
                      padding-top: 10px;		/*  */    \
                      background-repeat: no-repeat;	         /* 设置背景图像的平铺模式 */\
                      /* 按钮文本设置 */\
                      text-align: content;			             /* 文本的位置 */\
                      font-size: 18px;\
                      color: #FFFFFF;			                 /* 文本颜色 */\
                 "
);

//
the_btn_style[2]=("QPushButton:pressed{background-color:rgb(99,211,133);}");
the_btn_style[1]=("\
                  outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                  border-radius: 8px;		                 /* 按钮边框的圆角设置 */\
                  border-style: outset;\
                  /* 按钮背景图标设置 */\
                  background-image: url(:/pic/Airflow_mode/等待.png); /* 背景图片 */\
                  background-origin: content;\
                  background-position: left;	         /* 背景图片的位置 */\
                  padding-right: 0px;	    /*内边距设置*/	    \
                  padding-left: 10px;		/*  */	\
                  padding-bottom: 10px;	/*  */   \
                  padding-top:10px;		/*  */    \
                  background-repeat: repeat;	         /* 设置背景图像的平铺模式 */\
                  /* 按钮文本设置 */\
                  text-align:content ;			             /* 文本的位置 */\
                  font-size: 19px;\
                  color: rgb(225, 225, 225);			                 /* 文本颜色 */\
                  ");

                  for(int i=0;i<8;i++)//创建主屏幕显示的按键
                  {
                       theBtn[i] = new QPushButton(this);
              //        newbutton->setGeometry(30+(i*150),260,120,90 );//设定每隔放置一个按键
                       theBtn[i]->setFixedSize(145,38);
                      theBtn[i]->geometry();
                      btnlist_ui.append(theBtn[i]);
                  }
                  theBtn[0]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/GESYTEC系列号.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/GESYTEC系列号.png); /* 背景图片 */"+"}");
                  theBtn[1]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/TCP IP设置.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/TCP IP设置.png); /* 背景图片 */"+"}");
                  theBtn[2]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/仪器识别号.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/仪器识别号.png); /* 背景图片 */"+"}");
                  theBtn[3]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/数据流配置.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/数据流配置.png); /* 背景图片 */"+"}");
                  theBtn[4]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/通讯协议.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/通讯协议.png); /* 背景图片 */"+"}");
                  theBtn[5]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/串口设置.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/串口设置.png); /* 背景图片 */"+"}");
                  theBtn[6]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/返回.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/返回2.png); /* 背景图片 */"+"}");
                  theBtn[7]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/返回.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/返回2.png); /* 背景图片 */"+"}");

                 btnlist_ui.at(0)->setText(tr("     内部电磁阀1"));
                 btnlist_ui.at(1)->setText(tr("     内部电磁阀2"));
                 btnlist_ui.at(2)->setText(tr("     内部电磁阀3"));
                 btnlist_ui.at(3)->setText(tr("     内部电磁阀4"));
                 btnlist_ui.at(4)->setText(tr("     内部电磁阀5"));
                 btnlist_ui.at(5)->setText(tr("     内部电磁阀6"));
                 btnlist_ui.at(6)->setText(tr("     内部电磁阀7"));
                 btnlist_ui.at(7)->setText(tr("     返回"));


                  for(int i=0;i<19;i++)//创建主屏幕显示的按键
                  {
                       theLabel[i] = new QLabel(this);
                       theLabel[i]->setStyleSheet("QLabel{\
                                              outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                                              border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                                              border-style: outset;\
                                              /* 按钮文本设置 */\
                                              qproperty-alignment: 'AlignCenter| AlignCenter';			             /* 文本的位置 */\
                                              font-size: 20px;\
                                              color: #123567;			                 /* 文本颜色 */\
                                         }"
                        );
                  }
                  theLabel[0]->setStyleSheet("QLabel{ image: url(:/pic/logo.png); background-color: rgb(20, 170, 225);}"    );
                  theLabel[0]->setStyleSheet("QLabel{"
                                        + the_StyleSheet[20]+ " font-size: 25px;"+ "background-color: rgb(20, 70, 255);	    /* 背景颜色 */"+  "}"    );
                  theLabel[1]->setStyleSheet("QLabel{ image: url(:/pic/左.png); }"    );
                  theLabel[2]->setStyleSheet("QLabel{ image: url(:/pic/右.png); }"    );

                 theLabel[0]->setGeometry(0,0,640,60);//设定每隔放置一个按键
                 theLabel[1]->setGeometry(0,0,300,60);//设定每隔放置一个按键
                 theLabel[2]->setGeometry(340,0,300,60);//设定每隔放置一个按键
                 theLabel[0]->setText("串口设置");
                 theLabel[15]->setText("设置状态");
                 theLabel[16]->setText("当前状态");

                 //事件过滤器
                 foreach (QPushButton *btn, btnlist_ui) {
                     btn->installEventFilter(this);
                 }

                 QGridLayout *theLayout = new QGridLayout();
//                 theLayout->setAlignment(Qt::AlignCenter);
                 theLayout->addWidget(btnlist_ui.at(0), 1, 0);
                 theLayout->addWidget(btnlist_ui.at(1), 2, 0);
                 theLayout->addWidget(btnlist_ui.at(2), 3, 0);
                 theLayout->addWidget(btnlist_ui.at(3), 4, 0);
                 theLayout->addWidget(btnlist_ui.at(4), 5, 0);
                 theLayout->addWidget(btnlist_ui.at(5), 6, 0);
                 theLayout->addWidget(btnlist_ui.at(6), 7, 0);
                 theLayout->addWidget(btnlist_ui.at(7), 8, 0);
                 theLayout->addWidget(theLabel[3], 1, 2);
                 theLayout->addWidget(theLabel[4], 2, 2);
                 theLayout->addWidget(theLabel[5], 3, 2);
                 theLayout->addWidget(theLabel[6], 4, 2);
                 theLayout->addWidget(theLabel[7], 5, 2);
                 theLayout->addWidget(theLabel[8], 6, 2);
                 theLayout->addWidget(theLabel[9], 1, 3);
                 theLayout->addWidget(theLabel[10], 2, 3);
                 theLayout->addWidget(theLabel[11], 3, 3);
                 theLayout->addWidget(theLabel[12], 4, 3);
                 theLayout->addWidget(theLabel[13], 5, 3);
                 theLayout->addWidget(theLabel[14], 6, 3);
                 theLayout->addWidget(theLabel[17], 7, 2);
                 theLayout->addWidget(theLabel[18], 7, 3);
                 theLayout->addWidget(theLabel[15], 0, 2);
                 theLayout->addWidget(theLabel[16], 0, 3);
                 theLayout->setContentsMargins(50,90,50,50);//(int left, int top, int right, int bottom)
                 theLayout->setSizeConstraint(QLayout::SetMinimumSize);
                   setLayout(theLayout);

                   QWidget* widget = new QWidget(this);
                   widget->setGeometry(0,0,640,330);
                   widget->setLayout(theLayout);
                   }
void elec_Show_Outelec::update_data()
                   {
                       //1
                       if(elec_show_flag2[0]=="开")
                               theLabel[3]->setText(tr("开"));
                       else
                           theLabel[3]->setText(tr("关"));
                       //2
                       if(elec_show_flag2[1]=="开")
                           theLabel[4]->setText(tr("开"));
                       else
                             theLabel[4]->setText(tr("关"));
                        //3
                       if(elec_show_flag2[2]=="开")
                               theLabel[5]->setText(tr("开"));
                       else
                           theLabel[5]->setText(tr("关"));
                       //4
                       if(elec_show_flag2[3]=="开")
                               theLabel[6]->setText(tr("开"));
                       else
                           theLabel[6]->setText(tr("关"));
                       //5
                       if(elec_show_flag2[4]=="开")
                               theLabel[7]->setText(tr("开"));
                       else
                           theLabel[7]->setText(tr("关"));
                       //6
                       if(elec_show_flag2[5]=="开")
                          theLabel[8]->setText(tr("开"));
                       else
                          theLabel[8]->setText(tr("关"));
                       if(elec_show_flag2[6]=="开")
                          theLabel[17]->setText(tr("开"));
                       else
                          theLabel[17]->setText(tr("关"));
                       //显示状态
                       ss1 = QString("%1").arg(Sysstatus->Int_W_Valves[0]);
                       theLabel[9]->setText(ss1);
                      ss2 = QString("%1").arg(Sysstatus->Int_W_Valves[1]);
                       theLabel[10]->setText(ss2);
                       ss3 = QString("%1").arg(Sysstatus->Int_W_Valves[2]);
                       theLabel[11]->setText(ss3);
                        ss4 = QString("%1").arg(Sysstatus->Int_W_Valves[3]);
                       theLabel[12]->setText(ss4);
                       ss5 = QString("%1").arg(Sysstatus->Int_W_Valves[4]);
                       theLabel[13]->setText(ss5);
                       ss6 = QString("%1").arg(Sysstatus->Int_W_Valves[5]);
                       theLabel[14]->setText(ss6);
                       ss7 = QString("%1").arg(Sysstatus->Int_W_Valves[6]);
                       theLabel[18]->setText(ss7);

                   }


void elec_Show_Outelec::connect_Init()
{

    //定时器
    connect(timer,&QTimer::timeout,[=](){
update_data();
    });

    connect(btnlist_ui.at(0),&QPushButton::clicked,[=](){
        if(elec_show_flag2[0]=="开")
            {
                Sysctrl->BD_Interface.W_ValveState[0]=0;
                theLabel[3]->setText(tr("关"));
                elec_show_flag2[0]="关";
             }
        else
        {
            Sysctrl->BD_Interface.W_ValveState[0]=1;
            theLabel[3]->setText(tr("开"));
            elec_show_flag2[0]="开";
         }
    });

    connect(btnlist_ui.at(1),&QPushButton::clicked,[=](){
        if(elec_show_flag2[1]=="开")
            {
            Sysctrl->BD_Interface.W_ValveState[1]=0;
                theLabel[4]->setText(tr("关"));
                elec_show_flag2[1]="关";
             }
        else
        {
            Sysctrl->BD_Interface.W_ValveState[1]=1;
            theLabel[4]->setText(tr("开"));
            elec_show_flag2[1]="开";
         }
    });

    connect(btnlist_ui.at(2),&QPushButton::clicked,[=](){
        if(elec_show_flag2[2]=="开")
            {
            Sysctrl->BD_Interface.W_ValveState[2]=0;
                theLabel[5]->setText(tr("关"));
                elec_show_flag2[2]="关";
             }
        else
        {
            Sysctrl->BD_Interface.W_ValveState[2]=1;
            theLabel[5]->setText(tr("开"));
            elec_show_flag2[2]="开";
         }
    });

    connect(btnlist_ui.at(3),&QPushButton::clicked,[=](){
        if(elec_show_flag2[3]=="开")
            {
            Sysctrl->BD_Interface.W_ValveState[3]=0;
                theLabel[6]->setText(tr("关"));
                elec_show_flag2[3]="关";
             }
        else
        {
            Sysctrl->BD_Interface.W_ValveState[3]=1;
            theLabel[6]->setText(tr("开"));
            elec_show_flag2[3]="开";
         }
    });
    connect(btnlist_ui.at(4),&QPushButton::clicked,[=](){
        if(elec_show_flag2[4]=="开")
            {
            Sysctrl->BD_Interface.W_ValveState[4]=0;
                theLabel[7]->setText(tr("关"));
                elec_show_flag2[4]="关";
             }
        else
        {
            Sysctrl->BD_Interface.W_ValveState[4]=1;
            theLabel[7]->setText(tr("开"));
            elec_show_flag2[4]="开";
         }

    });

//通讯协议 0-clink 1-modbus
    connect(btnlist_ui.at(5),&QPushButton::clicked,[=](){
        if(elec_show_flag2[5]=="开")
            {
            Sysctrl->BD_Interface.W_ValveState[5]=0;
                theLabel[8]->setText(tr("关"));
                elec_show_flag2[5]="关";
             }
        else
        {
            Sysctrl->BD_Interface.W_ValveState[5]=1;
            theLabel[8] ->setText(tr("开"));
            elec_show_flag2[5]="开";
         }

    });

    connect(btnlist_ui.at(6),&QPushButton::clicked,[=](){
        if(elec_show_flag2[6]=="开")
            {
            Sysctrl->BD_Interface.W_ValveState[6]=0;
                theLabel[17]->setText(tr("关"));
                elec_show_flag2[6]="关";
             }
        else
        {
            Sysctrl->BD_Interface.W_ValveState[6]=1;
            theLabel[17]->setText(tr("开"));
            elec_show_flag2[6]="开";
         }

    });

    //返回按钮
    connect(btnlist_ui.at(7),&QPushButton::clicked,[=](){
        this->parentWidget()->show();
        this->close();
        Sysctrl->Para_Save = 1;
    });




}

bool elec_Show_Outelec::eventFilter(QObject *watched, QEvent *event)
    {

            for(int i=0;i<btnlist_ui.length();i++)
                if(btnlist_ui.at(i) == watched)
            {
                if(event->type() == QEvent::KeyPress)
                {
                   QKeyEvent *ev = (QKeyEvent *)event;
                   QPushButton *button = (QPushButton *)watched;
                   switch (ev->key())
                   {
                   case OK:
                     {
                       emit  button->clicked();
                       break;
                     }
                   case UP://上
                   {
                       if(focusWidget() != btnlist_ui.first())
                       focusPreviousChild();
                       break;
                   }
                   case DOWN://下
                   {
                       if(focusWidget() != btnlist_ui.back())
                       focusNextChild();
                       break;
                   }
                   case LEFT://左
                   {
                     if(focusWidget() != btnlist_ui.first())
                          focusPreviousChild();
                     break;
                   }
                   case RIGHT://右
                   {
                     if(focusWidget() != btnlist_ui.back())
                          focusNextChild();
                  break;
                   }
                   case INIT: //打开初始化界面
                   {
                       init_Show *show = new init_Show(this);
                       show->show();
                       this->hide();
                       break;
                   }
                   case BACK:
                   {
                       this->parentWidget()->show();
                       this->close();
                       break;
                   }
                   default:
                       break;
                   }
                }

                }
           return QMainWindow::eventFilter(watched,event);//返回父类的事件过滤器

    }
